package net.garbage;

import model.eval.TestExcercise;
import net.UserData;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author Jose Martin Rozanec;
 */
public class ObjectClient extends Thread {
    private Socket socket = null;
    private ObjectOutputStream output = null;
    private ObjectInputStream input = null;

    private long clientID;
    private Token<Object> currentToken;

    public void connect(String IP, UserData data) {
        socket = null;
        try {
            socket = new Socket(IP, 1234);
            output = new ObjectOutputStream(socket.getOutputStream());
            input = new ObjectInputStream(socket.getInputStream());
            try {
                clientID = (Long) input.readObject();//assumes that the server sends an ID.
                data.setID(clientID);
                output.writeObject(data);
                TestExcercise test = (TestExcercise) input.readObject();
                output.writeObject(clientID);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection");
            System.exit(1);
        }
    }

    public void readInput() {
        try {
            currentToken = (Token<Object>) input.readObject();
            System.out.println("[ObjectClient] OK! - " + currentToken.getClientID());
            if (currentToken.getClientID() == clientID) {
                System.out.println("[ObjectClient] Received: ." + currentToken.getInformation().toString());
                System.out.println("Preparing to sleep...");//todo delete
                try {
                    sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("[ObjectClient] I woke up and is all right now!");//todo delete
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * Sends object to server. Never works with tokens. This are made by the server.
     *
     * @param object
     */
    public void writeOutput(Object object) {
        try {
            output.writeObject(object);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public long getClientID() {
        return clientID;
    }

    private void closeConnections() {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws IOException {

        Socket socket = null;
        ObjectOutputStream out = null;
        ObjectInputStream input = null;

        try {
            socket = new Socket("127.0.0.1", 1234);

            out = new ObjectOutputStream(socket.getOutputStream());
            input = new ObjectInputStream(socket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host");
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection");
            System.exit(1);
        }

        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        String num1, num2;

        //System.output.println(input.readLine()); //Uncomment to debug
        //todo ver uso de readFully de DataInputStream para obtener el objeto serializable.
        System.out.print("This int-->");
        num1 = read.readLine();
        out.writeObject(num1);
        String token = null;
        try {
            token = (String) input.readObject();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        while (token == null) {
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("Error while sleeping...");
                e.printStackTrace();
            }
            try {
                token = (String) input.readObject();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Token: ." + token);
        System.out.print("Times this int-->");
        num2 = read.readLine();
        out.writeObject(num2);
        System.out.println("Equals");

        System.out.println(input.readLine());

        out.close();
        input.close();
        read.close();
        socket.close();
    }
}
